<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="category-app">
    <div class="container">
        <div class="tableBar" style="display: inline-block">
            <el-button type="primary" @click="addClass()">
                + 新增故障分类
            </el-button>
        </div>
        <!--      表格-->
        <el-table :data="tableData" stripe class="tableBox">
            <el-table-column prop="name" label="故障名称"/></el-table-column>
            <el-table-column prop="count" label="设备数量"></el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="editHandle(scope.row)">
                        修改
                    </el-button>
                    <el-button type="text" size="small" class="delBut non" @click="deleteHandle(scope.row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>




        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>


    <!--    编辑对话框-->
    <el-dialog :title="classData.title" :visible.sync="classData.dialogVisible" width="30%" :before-close="handleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="分类名称：">
                <el-input
                        v-model="classData.name"
                        placeholder="请输入分类名称"
                        maxlength="14"
                />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="classData.dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="medium" @click="submitForm()">确 定</el-button>
        <el-button v-if="action != 'edit'" type="primary" size="medium" class="continue" @click="submitForm('go')"> 保存并继续添加 </el-button>
      </span>
    </el-dialog>


</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/notification.js"></script>
<script>
    new Vue({
        el: '#category-app',
        data() {
            return {


                //控制编辑对话框是否有保存并继续添加按钮
                action: '',


                //分页数据
                counts: 0,
                page: 1,
                pageSize: 10,


                tableData: [],
                //控制编辑对话框
                classData: {
                    'title': '添加故障分类',
                    'dialogVisible': false,
                    'NotificationId': '',
                    'name': '',

                }
            }
        },
        computed: {},
        created() {
            this.init()
        },
        mounted() {
        },
        methods: {


            /**
             * 初始化,根据page,pageSize从后台请求分页数据
             * 在初始化,页面大小,页码改变时请求
             * @returns {Promise<void>}
             */
            async init() {
                await getNotificationPage({'page': this.page, 'pageSize': this.pageSize}).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records
                        this.counts = Number(res.data.total)
                        console.log("notification",this.tableData)

                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },


            /**
             *
             */
            handleQuery() {
                this.page = 1;
                this.init();
            },
            /**
             * 点击添加按钮时调用
             *
             */
            addClass() {
                this.classData.title = "新增故障分类"
                this.action = 'add'
                this.classData.name = ''

                this.classData.dialogVisible = true
            },
            /**
             * 点击编辑按钮时调用
             * @param dat 待编集数据对象
             */
            editHandle(dat) {
                this.classData.title = '修改分类'
                this.action = 'edit'

                //用于回显
                this.classData.name = dat.name
                this.classData.id = dat.id


                this.classData.dialogVisible = true
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteNotification(id).then(res => {


                        //删除成功,关闭弹窗,并更新
                        if (res.code === 1) {
                            this.$message.success('删除成功！')
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            /**
             * 提交表单
             * @param st 在添加时根据是否存在来判断是否继续添加
             */
            submitForm(st) {
                const classData = this.classData
                const valid = (classData.name === 0 || classData.name)
                if (this.action === 'add') {
                    if (valid) {


                            addNotification({'name': classData.name, 'type': this.type}).then(res => {
                                console.log(res)
                                if (res.code === 1) {

                                    //添加成功,清空编辑表单数据,并更新
                                    this.$message.success('分类添加成功！')
                                    if (!st) {
                                        this.classData.dialogVisible = false
                                    } else {
                                        this.classData.name = ''
                                        this.classData.sort = ''
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })


                    } else {
                        this.$message.error('请输入分类名称或排序')
                    }
                }
                //修改
                else if (valid) {

                        editNotification({
                            'id': this.classData.id,
                            'name': this.classData.name,
                        }).then(res => {

                            if (res.code === 1) {
                                this.$message.success('分类修改成功！')
                                this.classData.dialogVisible = false
                                this.handleQuery()
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })
                    }


            },


            /**
             * 页面大小改变时调用
             * @param val
             */
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },

            /**
             * 页面页面改变时调用
             * @param val
             */
            handleCurrentChange(val) {
                this.page = val
                this.init()
            }
        }
    })
</script>
</body>
</html>